package com.hys.ultrasound.business.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.KeyboardUtils;
import com.hys.ultrasound.R;
import com.hys.ultrasound.data.entity.Goods;
import com.lpjeremy.uimodule.dialog.BaseDialog;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @desc:零散数弹窗
 * @date:2019/4/26 11:37
 * @auther:lp
 * @version:1.1.6
 */
public class ScatteredDialog extends BaseDialog implements View.OnClickListener {
    @BindView(R.id.btnCancel)
    TextView mBtnCancel;
    @BindView(R.id.btnSure)
    TextView mBtnSure;
    EditText mEdtThree;
    @BindView(R.id.txtTitle)
    TextView mTxtTitle;

    private Goods goods;

    private String beforeText, afterText;

    public ScatteredDialog(@NonNull Context context, final Goods goods) {
        super(context, false);
        ButterKnife.bind(this);
//        this.goods = goods;
//        mTxtTitle.setText(this.goods.getSpmch());
//        mBtnCancel.setOnClickListener(this);
//        mBtnSure.setOnClickListener(this);
//
//        mProBarLayout.setAlpha(0.5f);
//
//        mEdtOne.setOnEditTextChangedAndActionListener(new ScanEditText.OnEditTextChangedAndActionListener() {
//            @Override
//            public void onTextChanged(String value) {
//                getPiHao(goods, mEdtOne.getText().toString().trim(), false);
//            }
//
//            @Override
//            public void onEnterAction(String value) {
//                getPiHao(goods, mEdtOne.getText().toString().trim(), false);
//            }
//        });
    }

    @Override
    protected int getContentLayoutId() {
        return 0;//R.layout.dialog_scattered_num;
    }

    @Override
    protected boolean getCancelable() {
        return false;
    }

    /**
     * 获取批号
     *
     * @param goods
     * @param tiaoma
     */
    public void getPiHao(Goods goods, final String tiaoma, final boolean bool) {
        if (goods == null) {
            showCheckToast("请先选中要扫的商品");
            return;
        }
        if (TextUtils.isEmpty(tiaoma)) {
            showCheckToast("请录入条码");
            return;
        }
        if (tiaoma.length() != 16 && tiaoma.length() != 22 && tiaoma.length() != 18 && tiaoma.length() != 19 && tiaoma.length() != 17) {
            showCheckToast("条码错误，条码长度不正确!");
            return;
        }
        String threeTm = tiaoma.length() > 3 ? tiaoma.substring(0, 3) : tiaoma;
        if (!TextUtils.equals(goods.getSpxsbh(), threeTm)) {
            showCheckToast("条码产品与实际产品不一致，请换品种！");
            return;
        }
        //保存时候判断 不相等 调用接口 拿到密码
        if (goods.getYiwchsl() == goods.getShl()) {
            showCheckToast("该品种件数已扫描完成，请换品种！");
            return;
        }
        //此处还有个判断是否重复条码 "该条码已扫描过，请检查。"
//        if (doesItExist(goods.getScanList(), tiaoma)) {
//            showCheckToast("该条码已扫描过，请检查。");
//            return;
//        }

        String pihao = "";
        if (tiaoma.length() == 16) {
            pihao = tiaoma.substring(3, 9);//取6位
            getPiHaoSuccess(tiaoma, pihao, bool);
        } else if (tiaoma.length() == 17) {
            pihao = tiaoma.substring(3, 10);//取7位
            getPiHaoSuccess(tiaoma, pihao, bool);
        } else if (tiaoma.length() == 18 || tiaoma.length() == 19) {
            showLoading();
        } else if (tiaoma.length() == 22) {
            pihao = tiaoma.substring(3, 9) + ":" + tiaoma.substring(9, 12) + "/" + tiaoma.substring(12, 18) + ":" + tiaoma.substring(18, 21);
            getPiHaoSuccess(tiaoma, pihao, bool);
        } else {
            getPiHaoSuccess(tiaoma, pihao, bool);
        }
    }

    private void showLoading() {
//        mProBarLayout.setVisibility(View.VISIBLE);
    }

    private void hideLoading() {
//        mProBarLayout.setVisibility(View.GONE);
    }

    private void getPiHaoSuccess(String tiaoma, String value, boolean bool) {
//        mEdtTwo.setText(value);
        if (bool) {
            resultData(tiaoma, value);
        } else {
            mEdtThree.requestFocus();
            KeyboardUtils.showSoftInput(mEdtThree);
        }
    }

    private void showCheckToast(String value) {
        showToast(value);
    }

    /**
     * 判断扫码记录是否存储此条码
     *
     * @param list
     * @param barCode
     * @return
     */
    private boolean doesItExist(List<Object> list, String barCode) {
        return false;
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btnSure:
                String barCode = "";//mEdtOne.getText().toString().trim();
                if (TextUtils.isEmpty(barCode)) {
                    showCheckToast("请录入条码");
                    return;
                }
                String piHao = "";// mEdtTwo.getText().toString().trim();
                if (TextUtils.isEmpty(piHao)) {
                    getPiHao(goods, barCode, true);
                    return;
                }
                resultData(barCode, piHao);

                break;
            case R.id.btnCancel:
                KeyboardUtils.hideSoftInput(view);
                dismiss();
                break;
        }
    }

    private void resultData(String tiaoMa, String piHao) {
        String countStr = mEdtThree.getText().toString().trim();
        if (TextUtils.isEmpty(countStr)) {
            showCheckToast("请输入零散数量");
            return;
        }
        int count = Integer.valueOf(countStr);
        int sum = goods.getYiwchsl() + count;
        if (sum > goods.getShl()) {
            showCheckToast("输入数量不能超过应扫总数量");
            return;
        }
    }
}
